package com.zhangyx.crawler.core;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.zhangyx.crawler.comm.CacheUtil;
import org.apache.log4j.Logger;

import java.util.Map;

public class GAEUtil {
    private static Gson gson = new Gson();

    private static Logger log = Logger.getLogger(GAEUtil.class);

    public static String[] grab(String url, Map<String, String> headers, Map<String, Object> data) {
        String[] ret = {"200", ""};

        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
        Map<String, String> map = Maps.newHashMap();
        map.put("down_method", "get");
        map.put("down_url", url);
        map.put("down_header", gson.toJson(headers));
        map.put("data", gson.toJson(data));
        try {
            String proxyUrl = CacheUtil.getProxyUrl();
            HttpClientUtil downloader = HttpClientUtil.POST(proxyUrl + "down")
                    .setProxy("127.0.0.1", 1080,"http")
                    .addEntities(map)
                    .start();

            ret[0] = String.valueOf(downloader.getResponseCode());
            ret[1] = downloader.getHtml();
        } catch (Exception e) {
            log.error("gae代理下载发生错误:" + e.getMessage());
        }
        return ret;
    }
}
